package com.client.job.utils;



import java.util.Date;

import org.springframework.scheduling.support.CronSequenceGenerator;


/**
 * cron解析工具
 *
 * @author monxz
 * @Date 2021年3月1日上午11:47:04
 * @Des
 *
 */
public class CronUtils {

	/**
	 * 获取到下次执行间隔
	 * 
	 * @param cron
	 * @return
	 */
	public static Long getNextSenconds(String cron) {
		Date nowTime = new Date();
		CronSequenceGenerator cronSequenceGenerator = new CronSequenceGenerator(cron);
		Date nextTimePoint = cronSequenceGenerator.next(nowTime);
		return (nextTimePoint.getTime() - nowTime.getTime()) / 1000;
	}
	
	public static Long getNextSenconds(String cron,Date nowTime) {
		CronSequenceGenerator cronSequenceGenerator = new CronSequenceGenerator(cron);
		Date nextTimePoint = cronSequenceGenerator.next(nowTime);
		return (nextTimePoint.getTime() - nowTime.getTime()) / 1000;
	}
	
	public static Long getCronTimes(String cron) {
		Date nowRime = new Date();		
		CronSequenceGenerator cronSequenceGenerator = new CronSequenceGenerator(cron);
		Date d1 = cronSequenceGenerator.next(nowRime);
		Date d2= cronSequenceGenerator.next(d1);
		return (d2.getTime() - d1.getTime() )/1000;
	}
	
	
	
	
	
	
	
	/**
	 * 是否是秒级别
	 * @param cron
	 * @return
	 */
	public static boolean isSeconds(String cron) {
		Long seconds = getNextSenconds(cron);
		if (seconds > 60) {
			return false;
		}
		return true;
	}

}
